/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.promo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import model.promo.PromoPrice;
import utility.com.TextComponentUtils;

/**
 *
 * @author jay
 */
public class PromoPriceTM extends AbstractTableModel {
    
  //  private static final long serialVersionUID = 1L;\
  
    private List<PromoPrice> data;

    public PromoPriceTM(List<PromoPrice> data) {
        this.data = data;
    }

    public PromoPriceTM() {
        this(new ArrayList<PromoPrice>(0));
    }

    public PromoPrice get(int index) {
        return data.get(index);
    }

    public void add(PromoPrice var) {
        data.add(var);
        fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
    }

    public void remove(int index) {
        data.remove(index);
        fireTableRowsDeleted(index, index);
    }

    public void set(int index, PromoPrice var) {
        data.set(index, var);
        fireTableRowsUpdated(index, index);
    }

    public void clear() {
        data.clear();
        fireTableDataChanged();
    }

    public void add(Collection<PromoPrice> vars) {
        data.addAll(vars);
        fireTableRowsInserted(getRowCount() - (vars.size()), getRowCount() - 1);
    }
         
    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return 10;
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return (column==1 || column==5 || column==6 || column==7);
    }
    
    @Override
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 5: 
                BigDecimal mrg = TextComponentUtils.parseNumberToBigDecimal((String)value);
                data.get(rowIndex).setMargin(mrg);
                fireTableCellUpdated(rowIndex, columnIndex);
                break;
                
            case 6: 
                BigDecimal mrk = TextComponentUtils.parseNumberToBigDecimal((String)value);
                data.get(rowIndex).setMarkup(mrk);
                fireTableCellUpdated(rowIndex, columnIndex);
                break;                            
            
            case 7: 
                BigDecimal price = TextComponentUtils.parseNumberToBigDecimal((String)value);
                data.get(rowIndex).setPrice(price);
                fireTableCellUpdated(rowIndex, columnIndex);
                break;                                 
        }
    }
    
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0: return rowIndex+1;
            case 1: return data.get(rowIndex).getItem().getPlu();  
            case 2: return data.get(rowIndex).getItem().getNama();  
            case 3:  String displayNormal = TextComponentUtils.formatNumber(data.get(rowIndex).getSalesPrice());
                return displayNormal; 
                
            case 4:  String displayHpp = TextComponentUtils.formatNumber(data.get(rowIndex).getCostPrice());
                return displayHpp; 
            
            case 5:  String displayMrg = TextComponentUtils.formatNumberShowDecimal(data.get(rowIndex).getMargin(), 2);
                return displayMrg; 
            
            case 6:  String displayMrk = TextComponentUtils.formatNumber(data.get(rowIndex).getMarkup());
                return displayMrk; 
            
            case 7:  String displayPrice = TextComponentUtils.formatNumber(data.get(rowIndex).getPrice());
                return displayPrice; 
            
            case 8:  return data.get(rowIndex).getUpdated(); 
                
            default: return null;
        }
    }
    
    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0: return "NO";
            case 1: return "PLU";
            case 2: return "Keterangan";
            case 3: return "Hrg Normal";
            case 4: return "HPP+PPN";
            case 5: return "Mrg";
            case 6: return "Markup";
            case 7: return "Hrg Promo";
            case 8: return "";
            case 9: return "";
            default: return null;
        }
    }

    
}
